#include "tf_input.h"
#include <QApplication>
#include<QHBoxLayout>
#include<QDesktopWidget>
tf_input *tf_input::_instance = 0;
tf_input::tf_input(QWidget *parent) : QWidget(parent)
{
    this->Init_ui();
    this->InitProperty();
    this->InitForm();
    this->ChangeStyle();
}
void tf_input::Init_ui()
{
    btn0 =new QPushButton("0");
    btn00 =new QPushButton("00");
    btn1 =new QPushButton("1");
    btn2 =new QPushButton("2");
    btn3 =new QPushButton("3");
    btn4 =new QPushButton("4");
    btn5 =new QPushButton("5");
    btn6 =new QPushButton("6");
    btn7 =new QPushButton("7");
    btn8 =new QPushButton("8");
    btn9 =new QPushButton("9");
    btnDot =new QPushButton(".");
    btnDelete =new QPushButton("←");
    btnDelete->setObjectName("btnDelete");
    btnOther1 =new QPushButton("!");
    btnOther10 =new QPushButton("_");
    btnOther11 =new QPushButton("+");
    btnOther12 =new QPushButton("/");
    btnOther13 =new QPushButton("|");
    btnOther14 =new QPushButton(":");
    btnOther15 =new QPushButton("?");
    btnOther16 =new QPushButton("=");
    btnOther17 =new QPushButton(",");
    btnOther18 =new QPushButton("\\");
    btnOther19 =new QPushButton("(");
    btnOther2 =new QPushButton("@");
    btnOther20 =new QPushButton(")");
    btnOther21 =new QPushButton("\"");
    btnOther3 =new QPushButton("#");
    btnOther4 =new QPushButton("$");
    btnOther5 =new QPushButton("%");
    btnOther6 =new QPushButton("^");
    btnOther7 =new QPushButton("&&");
    btnOther7->setObjectName("btnOther7");
    btnOther8 =new QPushButton("*");
    btnOther9 =new QPushButton("-");
    btnSpace =new QPushButton("空格");
    btnSpace->setObjectName("btnSpace");
    btnType =new QPushButton("小写");
    btnType->setObjectName("btnType");
    btna =new QPushButton("a");
    btnb =new QPushButton("b");
    btnc =new QPushButton("c");
    btnd =new QPushButton("d");
    btne =new QPushButton("e");
    btnf =new QPushButton("f");
    btng =new QPushButton("g");
    btnh =new QPushButton("h");
    btni =new QPushButton("i");
    btnj =new QPushButton("j");
    btnk =new QPushButton("k");
    btnl =new QPushButton("l");
    btnm =new QPushButton("m");
    btnn =new QPushButton("n");
    btno =new QPushButton("o");
    btnp =new QPushButton("p");
    btnq =new QPushButton("q");
    btnr =new QPushButton("r");
    btns =new QPushButton("s");
    btnt =new QPushButton("t");
    btnu =new QPushButton("u");
    btnv =new QPushButton("v");
    btnw =new QPushButton("w");
    btnx =new QPushButton("x");
    btny =new QPushButton("y");
    btnz =new QPushButton("z");
    btnClose =new QPushButton("关闭");
    btnClose->setObjectName("btnClose");
    btnNext =new QPushButton(">");
    btnNext->setObjectName("btnNext");
    btnPre =new QPushButton("<");
    btnPre->setObjectName("btnPre");
    //labInfo = new QLabel("中文输入法--小写");
    labPY = new QLabel();
    labCh0=new QLabel();
    labCh1=new QLabel();
    labCh2=new QLabel();
    labCh3=new QLabel();
    labCh4=new QLabel();
    labCh5=new QLabel();
    labCh6=new QLabel();
    labCh7=new QLabel();
    labCh8=new QLabel();
    labCh9=new QLabel();
    QVBoxLayout *lay4= new QVBoxLayout;
    QVBoxLayout *lay_top= new QVBoxLayout;
    QHBoxLayout *lay1=new QHBoxLayout;
    lay1->addWidget(labInfo);
    lay1->addWidget(labPY);
    lay1->addWidget(btnPre);
    lay1->addWidget(btnNext);
    lay1->addWidget(btnClose);
    QHBoxLayout *lay2=new QHBoxLayout;//hanzi xianshi
    lay2->addWidget(labCh0);
    lay2->addWidget(labCh1);
    lay2->addWidget(labCh2);
    lay2->addWidget(labCh3);
    lay2->addWidget(labCh4);
    lay2->addWidget(labCh5);
    lay2->addWidget(labCh6);
    lay2->addWidget(labCh7);
    lay2->addWidget(labCh8);
    lay2->addWidget(labCh9);
    lay_top->addLayout(lay1);
    lay_top->addLayout(lay2);

    QBoxLayout *lay3= new QVBoxLayout;//shu zi jianpan
    QHBoxLayout *lay6=new QHBoxLayout;
    QHBoxLayout *lay7=new QHBoxLayout;
    QHBoxLayout *lay8=new QHBoxLayout;
    QHBoxLayout *lay9=new QHBoxLayout;
    lay6->addWidget(btn7);
    lay6->addWidget(btn8);
    lay6->addWidget(btn9);
    lay7->addWidget(btn4);
    lay7->addWidget(btn5);
    lay7->addWidget(btn6);
    lay8->addWidget(btn1);
    lay8->addWidget(btn2);
    lay8->addWidget(btn3);
    lay9->addWidget(btn0);
    lay9->addWidget(btn00);
    lay9->addWidget(btnDot);
    lay3->addLayout(lay6);
    lay3->addLayout(lay7);
    lay3->addLayout(lay8);
    lay3->addLayout(lay9);


    QVBoxLayout *lay5=new QVBoxLayout;//zimu
    QHBoxLayout *lay1_1= new QHBoxLayout;
    lay1_1->addWidget(btnOther1);
    lay1_1->addWidget(btnOther2);
    lay1_1->addWidget(btnOther3);
    lay1_1->addWidget(btnOther4);
    lay1_1->addWidget(btnOther5);
    lay1_1->addWidget(btnOther6);
    lay1_1->addWidget(btnOther7);
    lay1_1->addWidget(btnOther8);
    lay1_1->addWidget(btnOther19);
    lay1_1->addWidget(btnOther20);
    lay1_1->addWidget(btnOther9);
    lay1_1->addWidget(btnOther11);
    lay1_1->addWidget(btnDelete);
    QHBoxLayout *lay1_2= new QHBoxLayout;
    lay1_2->addWidget(btnq);
    lay1_2->addWidget(btnw);
    lay1_2->addWidget(btne);
    lay1_2->addWidget(btnr);
    lay1_2->addWidget(btnt);
    lay1_2->addWidget(btny);
    lay1_2->addWidget(btnu);
    lay1_2->addWidget(btni);
    lay1_2->addWidget(btno);
    lay1_2->addWidget(btnp);
    lay1_2->addWidget(btnOther10);
    lay1_2->addWidget(btnOther12);
    lay1_2->addWidget(btnOther13);
    QHBoxLayout *lay1_3= new QHBoxLayout;
    lay1_3->addWidget(btna);
    lay1_3->addWidget(btns);
    lay1_3->addWidget(btnd);
    lay1_3->addWidget(btnf);
    lay1_3->addWidget(btng);
    lay1_3->addWidget(btnh);
    lay1_3->addWidget(btnj);
    lay1_3->addWidget(btnk);
    lay1_3->addWidget(btnl);
    lay1_3->addWidget(btnOther14);
    lay1_3->addWidget(btnOther21);
    lay1_3->addWidget(btnOther15);
    lay1_3->addWidget(btnOther16);
    QHBoxLayout *lay1_4= new QHBoxLayout;
    lay1_4->addWidget(btnz);
    lay1_4->addWidget(btnx);
    lay1_4->addWidget(btnc);
    lay1_4->addWidget(btnv);
    lay1_4->addWidget(btnb);
    lay1_4->addWidget(btnn);
    lay1_4->addWidget(btnm);
    lay1_4->addWidget(btnOther17);
    lay1_4->addWidget(btnOther18);
    lay1_4->addWidget(btnSpace);
    lay1_4->addWidget(btnType);
    lay5->addLayout(lay1_1);
    lay5->addLayout(lay1_2);
    lay5->addLayout(lay1_3);
    lay5->addLayout(lay1_4);



    QHBoxLayout *lay_main=new QHBoxLayout;
    lay_main->addLayout(lay5);
    lay_main->addLayout(lay3);
    lay_main->setStretchFactor(lay5,4);
    lay_main->setStretchFactor(lay3,1);

    lay4->addLayout(lay_top);
    lay4->addLayout(lay_main);
    lay4->setStretchFactor(lay_top,1);
    lay4->setStretchFactor(lay_main,5);
    this->setLayout(lay4);
}

void tf_input::Init(QString position, QString style, int btnFontSize, int labFontSize)
{
    this->currentPosition = position;
    this->currentStyle = style;
    this->btnFontSize = btnFontSize;
    this->labFontSize = labFontSize;
    this->ChangeStyle();
    this->ChangeFont();
}

void tf_input::mouseMoveEvent(QMouseEvent *e)
{
    if (mousePressed && (e->buttons() && Qt::LeftButton)) {
        this->move(e->globalPos() - mousePoint);
        e->accept();
    }
}

void tf_input::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        mousePressed = true;
        mousePoint = e->globalPos() - this->pos();
        e->accept();
    }
}

void tf_input::mouseReleaseEvent(QMouseEvent *)
{
    mousePressed = false;
}

void tf_input::InitForm()
{
    this->mousePressed = false;
    this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);

    QDesktopWidget w;
    deskWidth = w.availableGeometry().width();
    deskHeight = w.availableGeometry().height();
    frmWidth = this->width();
    frmHeight = 50;

    QSqlDatabase DbConn;
    DbConn = QSqlDatabase::addDatabase("QSQLITE");
    qDebug()<<"applicationDirPath:"<<qApp->applicationDirPath()+ "/pinyin.db";
    DbConn.setDatabaseName(qApp->applicationDirPath() + "/pinyin.db");
    bool isSuccess = DbConn.open();
    if(!isSuccess)
    {
        qDebug()<<DbConn.lastError().text();
        qDebug("DbConn.open failed");
    }

    isFirst = true;
    isPress = false;
    timerPress = new QTimer(this);
    connect(timerPress, SIGNAL(timeout()), this, SLOT(reClicked()));

    currentWidget = 0;
    currentLineEdit = 0;
    currentTextEdit = 0;
    currentPlain = 0;
    currentBrowser = 0;
    currentEditType = "";

    //如果需要更改输入法面板的显示位置,改变currentPosition这个变量即可
    //control--显示在对应输入框的正下方 bottom--填充显示在底部  center--窗体居中显示
    currentPosition = "bottom";

    //如果需要更改输入法面板的样式,改变currentStyle这个变量即可
    //blue--淡蓝色  dev--dev风格  black--黑色  brown--灰黑色  lightgray--浅灰色  darkgray--深灰色  gray--灰色  silvery--银色
    currentStyle = "blue";

    //输入法面板字体大小,如果需要更改面板字体大小,该这里即可
    btnFontSize = 10;
    labFontSize = 10;

    //如果需要更改输入法初始模式,改变currentType这个变量即可
    //min--小写模式  max--大写模式  chinese--中文模式
    currentType = "min";
    changeType(currentType);

    QList<QPushButton *> btn = this->findChildren<QPushButton *>();
    foreach (QPushButton * b, btn) {
        connect(b, SIGNAL(clicked()), this, SLOT(btn_clicked()));
        //qDebug()<<b->objectName();
    }

    //绑定全局改变焦点信号槽
    connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),
            this, SLOT(focusChanged(QWidget *, QWidget *)));
    //绑定按键事件过滤器
    qApp->installEventFilter(this);
}

void tf_input::InitProperty()
{
    btnOther1->setProperty("btnOther", true);
    btnOther2->setProperty("btnOther", true);
    btnOther3->setProperty("btnOther", true);
    btnOther4->setProperty("btnOther", true);
    btnOther5->setProperty("btnOther", true);
    btnOther6->setProperty("btnOther", true);
    btnOther7->setProperty("btnOther", true);
    btnOther8->setProperty("btnOther", true);
    btnOther9->setProperty("btnOther", true);
    btnOther10->setProperty("btnOther", true);
    btnOther11->setProperty("btnOther", true);
    btnOther12->setProperty("btnOther", true);
    btnOther13->setProperty("btnOther", true);
    btnOther14->setProperty("btnOther", true);
    btnOther15->setProperty("btnOther", true);
    btnOther16->setProperty("btnOther", true);
    btnOther17->setProperty("btnOther", true);
    btnOther18->setProperty("btnOther", true);
    btnOther19->setProperty("btnOther", true);
    btnOther20->setProperty("btnOther", true);
    btnOther21->setProperty("btnOther", true);

    btnDot->setProperty("btnOther", true);
    btnSpace->setProperty("btnOther", true);
    btnDelete->setProperty("btnOther", true);

    btn0->setProperty("btnNum", true);
    btn1->setProperty("btnNum", true);
    btn2->setProperty("btnNum", true);
    btn3->setProperty("btnNum", true);
    btn4->setProperty("btnNum", true);
    btn5->setProperty("btnNum", true);
    btn6->setProperty("btnNum", true);
    btn7->setProperty("btnNum", true);
    btn8->setProperty("btnNum", true);
    btn9->setProperty("btnNum", true);
    btn00->setProperty("btnNum", true);

    btna->setProperty("btnLetter", true);
    btnb->setProperty("btnLetter", true);
    btnc->setProperty("btnLetter", true);
    btnd->setProperty("btnLetter", true);
    btne->setProperty("btnLetter", true);
    btnf->setProperty("btnLetter", true);
    btng->setProperty("btnLetter", true);
    btnh->setProperty("btnLetter", true);
    btni->setProperty("btnLetter", true);
    btnj->setProperty("btnLetter", true);
    btnk->setProperty("btnLetter", true);
    btnl->setProperty("btnLetter", true);
    btnm->setProperty("btnLetter", true);
    btnn->setProperty("btnLetter", true);
    btno->setProperty("btnLetter", true);
    btnp->setProperty("btnLetter", true);
    btnq->setProperty("btnLetter", true);
    btnr->setProperty("btnLetter", true);
    btns->setProperty("btnLetter", true);
    btnt->setProperty("btnLetter", true);
    btnu->setProperty("btnLetter", true);
    btnv->setProperty("btnLetter", true);
    btnw->setProperty("btnLetter", true);
    btnx->setProperty("btnLetter", true);
    btny->setProperty("btnLetter", true);
    btnz->setProperty("btnLetter", true);

    labCh.append(labCh0);
    labCh.append(labCh1);
    labCh.append(labCh2);
    labCh.append(labCh3);
    labCh.append(labCh4);
    labCh.append(labCh5);
    labCh.append(labCh6);
    labCh.append(labCh7);
    labCh.append(labCh8);
    labCh.append(labCh9);
    for (int i = 0; i < 10; i++) {
        labCh[i]->installEventFilter(this);
    }
}

void tf_input::ShowPanel()
{
    this->setVisible(true);

    int width = btn0->width();
    width = width > 40 ? width : 40;
    btnPre->setMinimumWidth(width);
    btnPre->setMaximumWidth(width);
    btnNext->setMinimumWidth(width);
    btnNext->setMaximumWidth(width);
    btnClose->setMinimumWidth(width);
    btnClose->setMaximumWidth(width);
}

//事件过滤器,用于识别鼠标单击汉字标签处获取对应汉字
bool tf_input::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress) {
        if (obj == labCh0) {
            setChinese(0);
        } else if (obj == labCh1) {
            setChinese(1);
        } else if (obj == labCh2) {
            setChinese(2);
        } else if (obj == labCh3) {
            setChinese(3);
        } else if (obj == labCh4) {
            setChinese(4);
        } else if (obj == labCh5) {
            setChinese(5);
        } else if (obj == labCh6) {
            setChinese(6);
        } else if (obj == labCh7) {
            setChinese(7);
        } else if (obj == labCh8) {
            setChinese(8);
        } else if (obj == labCh9) {
            setChinese(9);
        } else if (currentEditType != "" && obj != btnClose) {
            ShowPanel();
        }
        btnPress = (QPushButton *)obj;
        if (checkPress()) {
            isPress = true;
            timerPress->start(500);
        }
        return false;
    } else if (event->type() == QEvent::MouseButtonRelease) {
        btnPress = (QPushButton *)obj;
        if (checkPress()) {
            isPress = false;
            timerPress->stop();
        }
        return false;
    } else if (event->type() == QEvent::KeyPress) {
        //如果输入法窗体不可见,则不需要处理
        if (!isVisible()) {
            return QWidget::eventFilter(obj, event);
        }

        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        //Shift切换输入法模式,esc键关闭输入法面板,空格取第一个汉字,退格键删除
        //中文模式下回车键取拼音,中文模式下当没有拼音时可以输入空格
        if (keyEvent->key() == Qt::Key_Space) {
            if (labPY->text() != "") {
                setChinese(0);
                return true;
            } else {
                return false;
            }
        } else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
            insertValue(labPY->text());
            labPY->setText("");
            selectChinese();
            return true;
        } else if (keyEvent->key() == Qt::Key_Shift) {
            btnType->click();
            return true;
        } else if (keyEvent->key() == Qt::Key_Escape) {
            btnClose->click();
            return true;
        } else if (keyEvent->key() == Qt::Key_Backspace) {
            btnDelete->click();
            return true;
        } else if (keyEvent->text() == "+" || keyEvent->text() == "=") {
            if (labPY->text() != "") {
                btnNext->click();
                return true;
            } else {
                return false;
            }
        } else if (keyEvent->text() == "-" || keyEvent->text() == "_") {
            if (labPY->text() != "") {
                btnPre->click();
                return true;
            } else {
                return false;
            }
        } else if (keyEvent->key() == Qt::Key_CapsLock) {
            if (currentType != "max") {
                currentType = "max";
            } else {
                currentType = "min";
            }
            changeType(currentType);
            return true;
        } else {
            if (currentEditType == "QWidget") {
                return false;
            }
            QString key;
            if (currentType == "chinese") {
                key = keyEvent->text();
            } else if (currentType == "min") {
                key = keyEvent->text().toLower();
            } else if (currentType == "max") {
                key = keyEvent->text().toUpper();
            }
            QList<QPushButton *> btn = this->findChildren<QPushButton *>();
            foreach (QPushButton * b, btn) {
                if (b->text() == key) {
                    b->click();
                    return true;
                }
            }
        }
        return false;
    }
    return QWidget::eventFilter(obj, event);
}

bool tf_input::checkPress()
{
    //只有属于输入法键盘的合法按钮才继续处理
    bool num_ok = btnPress->property("btnNum").toBool();
    bool other_ok = btnPress->property("btnOther").toBool();
    bool letter_ok = btnPress->property("btnLetter").toBool();
    if (num_ok || other_ok || letter_ok) {
        return true;
    }
    return false;
}

void tf_input::reClicked()
{
    if (isPress) {
        timerPress->setInterval(30);
        btnPress->click();
    }
}

void tf_input::focusChanged(QWidget *oldWidget, QWidget *nowWidget)
{
    //qDebug() << "oldWidget:" << oldWidget << " nowWidget:" << nowWidget;
    if (nowWidget != 0 && !this->isAncestorOf(nowWidget)) {
        //在Qt5和linux系统中(嵌入式linux除外),当输入法面板关闭时,焦点会变成无,然后焦点会再次移到焦点控件处
        //这样导致输入法面板的关闭按钮不起作用,关闭后马上有控件获取焦点又显示.
        //为此,增加判断,当焦点是从有对象转为无对象再转为有对象时不要显示.
        //这里又要多一个判断,万一首个窗体的第一个焦点就是落在可输入的对象中,则要过滤掉
#ifndef __arm__
        if (oldWidget == 0x0 && !isFirst) {
            return;
        }
#endif

        isFirst = false;
        QWidget * pModalWidget = QApplication::activeModalWidget () ;
        QWidget * pPopupWidget = QApplication::activePopupWidget() ;
        QWidget * pWidget = QApplication::activeWindow() ;

        if (NULL != pModalWidget && pModalWidget->inherits("QDialog"))
        {
            Qt::WindowModality Modality = pModalWidget->windowModality();
          /*Qt::NonModal       The window is not modal and does not block input to other windows.
          非模态对话框

          Qt::WindowModal        The window is modal to a single window hierarchy and blocks input to its parent window, all grandparent windows, and all siblings of its parent and grandparent windows.
          窗口级模态对话框，即只会阻塞父窗口、父窗口的父窗口及兄弟窗口。（半模态对话框）

          Qt::ApplicationModal       The window is modal to the application and blocks input to all windows.
          应用程序级模态对话框，即会阻塞整个应用程序的所有窗口。（半模态对话框）
          */
            if(Qt::ApplicationModal == Modality)
            {
                //需要将输入法切换到最初的原始状态--小写,同时将之前的对象指针置为零
                currentWidget = 0;
                currentLineEdit = 0;
                currentTextEdit = 0;
                currentPlain = 0;
                currentBrowser = 0;
                currentEditType = "";
                currentType = "min";
                changeType(currentType);
                this->setVisible(false);
                return;
            }
        }
        if (nowWidget->inherits("QLineEdit")) {
            currentLineEdit = (QLineEdit *)nowWidget;
            currentEditType = "QLineEdit";
            //ShowPanel();
        } else if (nowWidget->inherits("QTextEdit")) {
            currentTextEdit = (QTextEdit *)nowWidget;
            currentEditType = "QTextEdit";
            //ShowPanel();
        } else if (nowWidget->inherits("QPlainTextEdit")) {
            currentPlain = (QPlainTextEdit *)nowWidget;
            currentEditType = "QPlainTextEdit";
            //ShowPanel();
        } else if (nowWidget->inherits("QTextBrowser")) {
            currentBrowser = (QTextBrowser *)nowWidget;
            currentEditType = "QTextBrowser";
            //ShowPanel();
        } else if (nowWidget->inherits("QComboBox")) {
            QComboBox *cbox = (QComboBox *)nowWidget;
            //只有当下拉选择框处于编辑模式才可以输入
            if (cbox->isEditable()) {
                currentLineEdit = cbox->lineEdit() ;
                currentEditType = "QLineEdit";
                //ShowPanel();
            }
        } /*else if (nowWidget->inherits("QSpinBox") ||
                   nowWidget->inherits("QDoubleSpinBox") ||
                   nowWidget->inherits("QDateEdit") ||
                   nowWidget->inherits("QTimeEdit") ||
                   nowWidget->inherits("QDateTimeEdit")) {
            currentWidget = nowWidget;
            currentEditType = "QWidget";
            //ShowPanel();
        }*/ else {
            //需要将输入法切换到最初的原始状态--小写,同时将之前的对象指针置为零
            currentWidget = 0;
            currentLineEdit = 0;
            currentTextEdit = 0;
            currentPlain = 0;
            currentBrowser = 0;
            currentEditType = "";
            currentType = "min";
            changeType(currentType);
            this->setVisible(false);
        }

        //根据用户选择的输入法位置设置-居中显示-底部填充-显示在输入框正下方
        if (currentPosition == "center") {
            QPoint pos = QPoint(deskWidth / 2 - frmWidth / 2, deskHeight / 2 - frmHeight / 2);
            this->setGeometry(pos.x(), pos.y(), frmWidth, frmHeight);
        } else if (currentPosition == "bottom") {
            this->setGeometry(0, deskHeight - frmHeight, deskWidth, frmHeight);
        } else if (currentPosition == "control") {
            QRect rect = nowWidget->rect();
            QPoint pos = QPoint(rect.left(), rect.bottom() + 2);
            pos = nowWidget->mapToGlobal(pos);
            this->setGeometry(100, 340, frmWidth, frmHeight);
        }
    }
}

void tf_input::changeType(QString type)
{
    if (type == "max") {
        changeLetter(true);
        btnType->setText("大写");
        //labInfo->setText("中文输入法--大写");
        btnOther12->setText("/");
        btnOther14->setText(":");
        btnOther17->setText(",");
        btnOther18->setText("\\");
        btnOther21->setText("\"");
    } else if (type == "min") {
        changeLetter(false);
        btnType->setText("小写");
        //labInfo->setText("中文输入法--小写");
        btnOther12->setText("/");
        btnOther14->setText(":");
        btnOther17->setText(",");
        btnOther18->setText("\\");
        btnOther21->setText("\"");
    } /*else {
        changeLetter(false);
        btnType->setText("中文");
        //labInfo->setText("中文输入法--中文");
        btnOther12->setText("。");
        btnOther14->setText("：");
        btnOther17->setText("，");
        btnOther18->setText("；");
        btnOther21->setText("“");
    }*/
    //每次切换到模式,都要执行清空之前中文模式下的信息
    clearChinese();
    labPY->setText("");
}

void tf_input::changeLetter(bool isUpper)
{
    QList<QPushButton *> btn = this->findChildren<QPushButton *>();
    foreach (QPushButton * b, btn) {
        if (b->property("btnLetter").toBool()) {
            if (isUpper) {
                b->setText(b->text().toUpper());
            } else {
                b->setText(b->text().toLower());
            }
        }
    }
}

void tf_input::selectChinese()
{
    clearChinese();
    QSqlQuery query;
    QString currentPY = labPY->text();
//    QString sql = "select [word] from [pinyin] where [pinyin]='" + currentPY + "';";
    QString sql = "select [chinese] from [hzpy] where [pinyin]='" + currentPY + "'order by type desc;";
    query.exec(sql);
    //逐个将查询到的字词加入汉字队列
    while(query.next()) {
        QString result = query.value(0).toString();
        QStringList text = result.split(" ");
        foreach (QString txt, text) {
            if (txt.length() > 0) {
                allPY.append(txt);
                currentPY_count++;
            }
        }
    }
    showChinese();
}

void tf_input::showChinese()
{
    //每个版面最多显示10个汉字
    int count = 0;
    currentPY.clear();
    for (int i = 0; i < 10; i++) {
        labCh[i]->setText("");
    }

    for (int i = currentPY_index; i < currentPY_count; i++)
    {
        if (count == 10) {
            break;
        }
        QString txt = QString("%1.%2").arg(count).arg(allPY[currentPY_index]);
        currentPY.append(allPY[currentPY_index]);
        labCh[count]->setText(txt);
        count++;
        currentPY_index++;
    }
    qDebug() << "currentPY_index:" << currentPY_index << "currentPY_count:" << currentPY_count;
}

void tf_input::btn_clicked()
{
    //如果当前焦点控件类型为空,则返回不需要继续处理
    if (currentEditType == "") {
        return;
    }
    QPushButton *btn = (QPushButton *)sender();
    QString objectName = btn->objectName();
    if (objectName == "btnType") {
        if (currentType == "min") {
            currentType = "max";
        } else if (currentType == "max") {
            currentType = "chinese";
        } else if (currentType == "chinese") {
            currentType = "min";
        }
        changeType(currentType);
    } else if (objectName == "btnDelete") {
        //如果当前是中文模式,则删除对应拼音,删除完拼音之后再删除对应文本输入框的内容
        if (currentType == "chinese") {
            QString txt = labPY->text();
            int len = txt.length();
            if (len > 0) {
                labPY->setText(txt.left(len - 1));
                selectChinese();
            } else {
                deleteValue();
            }
        } else {
            deleteValue();
        }
    } else if (objectName == "btnPre") {
        if (currentPY_index >= 20) {
            //每次最多显示10个汉字,所以每次向前的时候索引要减20
            if (currentPY_index % 10 == 0) {
                currentPY_index -= 20;
            } else {
                currentPY_index = currentPY_count - (currentPY_count % 10) - 10;
            }
        } else {
            currentPY_index = 0;
        }
        showChinese();
    } else if (objectName == "btnNext") {
        if (currentPY_index < currentPY_count - 1) {
            showChinese();
        }
    } else if (objectName == "btnClose") {
        this->setVisible(false);
    } else if (objectName == "btnSpace") {
        insertValue(" ");
    } else {
        QString value = btn->text();
        //如果是&按钮，因为对应&被过滤,所以真实的text为去除前面一个&字符
        if (objectName == "btnOther7") {
            value = value.right(1);
        }
        //当前不是中文模式,则单击按钮对应text为传递参数
        if (currentType != "chinese") {
            insertValue(value);
        } else {
            //中文模式下,不允许输入特殊字符,单击对应数字按键取得当前索引的汉字
            if (btn->property("btnOther").toBool()) {
                if (labPY->text().length() == 0) {
                    insertValue(value);
                }
            } else if (btn->property("btnNum").toBool()) {
                if (labPY->text().length() == 0) {
                    insertValue(value);
                } else if (objectName == "btn0") {
                    setChinese(0);
                } else if (objectName == "btn1") {
                    setChinese(1);
                } else if (objectName == "btn2") {
                    setChinese(2);
                } else if (objectName == "btn3") {
                    setChinese(3);
                } else if (objectName == "btn4") {
                    setChinese(4);
                } else if (objectName == "btn5") {
                    setChinese(5);
                } else if (objectName == "btn6") {
                    setChinese(6);
                } else if (objectName == "btn7") {
                    setChinese(7);
                } else if (objectName == "btn8") {
                    setChinese(8);
                } else if (objectName == "btn9") {
                    setChinese(9);
                }
            } else if (btn->property("btnLetter").toBool()) {
                labPY->setText(labPY->text() + value);
                selectChinese();
            }
        }
    }
}

void tf_input::insertValue(QString value)
{
    if (currentEditType == "QLineEdit") {
        currentLineEdit->insert(value);
    } else if (currentEditType == "QTextEdit") {
        currentTextEdit->insertPlainText(value);
    } else if (currentEditType == "QPlainTextEdit") {
        currentPlain->insertPlainText(value);
    } else if (currentEditType == "QTextBrowser") {
        currentBrowser->insertPlainText(value);
    } else if (currentEditType == "QWidget") {
        QKeyEvent keyPress(QEvent::KeyPress, 0, Qt::NoModifier, QString(value));
        QApplication::sendEvent(currentWidget, &keyPress);
    }
}

void tf_input::deleteValue()
{
    if (currentEditType == "QLineEdit") {
        currentLineEdit->backspace();
    } else if (currentEditType == "QTextEdit") {
        //获取当前QTextEdit光标,如果光标有选中,则移除选中字符,否则删除光标前一个字符
        QTextCursor cursor = currentTextEdit->textCursor();
        if(cursor.hasSelection()) {
            cursor.removeSelectedText();
        } else {
            cursor.deletePreviousChar();
        }
    } else if (currentEditType == "QPlainTextEdit") {
        //获取当前QTextEdit光标,如果光标有选中,则移除选中字符,否则删除光标前一个字符
        QTextCursor cursor = currentPlain->textCursor();
        if(cursor.hasSelection()) {
            cursor.removeSelectedText();
        } else {
            cursor.deletePreviousChar();
        }
    } else if (currentEditType == "QTextBrowser") {
        //获取当前QTextEdit光标,如果光标有选中,则移除选中字符,否则删除光标前一个字符
        QTextCursor cursor = currentBrowser->textCursor();
        if(cursor.hasSelection()) {
            cursor.removeSelectedText();
        } else {
            cursor.deletePreviousChar();
        }
    } else if (currentEditType == "QWidget") {
        QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier, QString());
        QApplication::sendEvent(currentWidget, &keyPress);
    }
}

void tf_input::setChinese(int index)
{
    int count = currentPY.count();
    if (count > index) {
        insertValue(currentPY[index]);
        //添加完一个汉字后,清空当前汉字信息,等待重新输入
        clearChinese();
        labPY->setText("");
    }
}

void tf_input::clearChinese()
{
    //清空汉字,重置索引
    for (int i = 0; i < 10; i++) {
        labCh[i]->setText("");
    }
    allPY.clear();
    currentPY.clear();
    currentPY_index = 0;
    currentPY_count = 0;
}

void tf_input::ChangeStyle()
{
    if (currentStyle == "blue") {
        changeStyle("#DEF0FE", "#C0DEF6", "#C0DCF2", "#386487");
    } else if (currentStyle == "dev") {
        changeStyle("#C0D3EB", "#BCCFE7", "#B4C2D7", "#324C6C");
    } else if (currentStyle == "gray") {
        changeStyle("#E4E4E4", "#A2A2A2", "#A9A9A9", "#000000");
    } else if (currentStyle == "lightgray") {
        changeStyle("#EEEEEE", "#E5E5E5", "#D4D0C8", "#6F6F6F");
    } else if (currentStyle == "darkgray") {
        changeStyle("#D8D9DE", "#C8C8D0", "#A9ACB5", "#5D5C6C");
    } else if (currentStyle == "black") {
        changeStyle("#4D4D4D", "#292929", "#D9D9D9", "#CACAD0");
    } else if (currentStyle == "brown") {
        changeStyle("#667481", "#566373", "#C2CCD8", "#E7ECF0");
    } else if (currentStyle == "silvery") {
        changeStyle("#E1E4E6", "#CCD3D9", "#B2B6B9", "#000000");
    }
}

void tf_input::ChangeFont()
{
    QFont btnFont(this->font().family(), btnFontSize);
    QFont labFont(this->font().family(), labFontSize);

    QList<QPushButton *> btns = this->findChildren<QPushButton *>();
    foreach (QPushButton * btn, btns) {
        btn->setFont(btnFont);
    }

    QList<QLabel *> labs = this->findChildren<QLabel *>();
    foreach (QLabel * lab, labs) {
        lab->setFont(labFont);
    }
    btnPre->setFont(labFont);
    btnNext->setFont(labFont);
    btnClose->setFont(labFont);
}

void tf_input::changeStyle(QString topColor, QString bottomColor, QString borderColor, QString textColor)
{
    QStringList qss;
    qss.append(QString("QWidget#frmInput{background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 %1,stop:1 %2);}")
               .arg(topColor).arg(bottomColor));
    qss.append("QPushButton{padding:5px;border-radius:3px;}");
    qss.append(QString("QPushButton:hover{background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 %1,stop:1 %2);}")
               .arg(topColor).arg(bottomColor));
    qss.append(QString("QLabel,QPushButton{color:%1;}").arg(textColor));
    qss.append(QString("QPushButton#btnPre,QPushButton#btnNext,QPushButton#btnClose{padding:5px;}"));
    qss.append(QString("QPushButton{border:1px solid %1;}")
               .arg(borderColor));
    qss.append(QString("QLineEdit{border:1px solid %1;border-radius:5px;padding:2px;background:none;selection-background-color:%2;selection-color:%3;}")
               .arg(borderColor).arg(bottomColor).arg(topColor));
    this->setStyleSheet(qss.join(""));
}


tf_input::~tf_input()
{

}
